package com.hexus.eve.actions;

import java.util.Collection;
import java.util.List;

import com.beimin.eveapi.account.accountstatus.AccountStatusParser;
import com.beimin.eveapi.account.accountstatus.AccountStatusResponse;
import com.beimin.eveapi.account.characters.CharactersParser;
import com.beimin.eveapi.account.characters.CharactersResponse;
import com.beimin.eveapi.character.assetlist.AssetListParser;
import com.beimin.eveapi.character.killlog.KillLogParser;
import com.beimin.eveapi.character.sheet.CharacterSheetParser;
import com.beimin.eveapi.character.sheet.CharacterSheetResponse;
import com.beimin.eveapi.core.ApiAuthorization;
import com.beimin.eveapi.core.ApiException;
import com.beimin.eveapi.shared.assetlist.AbstractAssetListParser;
import com.beimin.eveapi.shared.assetlist.ApiAsset;
import com.beimin.eveapi.shared.assetlist.AssetListResponse;
import com.beimin.eveapi.shared.killlog.ApiKill;
import com.beimin.eveapi.shared.killlog.KillLogResponse;
import com.hexus.eve.application.Constants;
import com.hexus.eve.entities.Account;
import com.hexus.eve.entities.MarketGroup;
import com.hexus.eve.entities.Type;
import com.hexus.eve.entities.security.User;
import com.hexus.eve.utils.CurrentUser;
import com.hexus.eve.utils.MarketGroupUtils;
import com.opensymphony.xwork2.ActionSupport;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;


@SuppressWarnings("serial")
public class GetBlueprints extends ActionSupport{
	@Autowired
	private SessionFactory sessionFactory;
	@Autowired
	private MarketGroupUtils marketGroupUtils;
	
	private Collection<ApiAsset<ApiAsset<?>>> assets;
	private KillLogResponse kilResponse;
	private CharactersResponse chrResponse;
	private AccountStatusResponse accResponse;
	private CharacterSheetResponse chrSheetResponse;
	private Integer charID = null;
	
	//private Cipher cipher;
	
	private Account a; 
	
	private void out(ApiAsset a, String level, HibernateTemplate ht) {
		Type t = ht.get(Type.class, a.getTypeID());
		
		if(t != null)
			System.out.println(level + t.getName());
		else
			System.out.println(level + "No such type!!! ID=" + a.getTypeID());
		
		if(a.getAssets().size() > 0) {
			for(Object aa : a.getAssets()) {
				out((ApiAsset)aa, level + "   ", ht);
			}
		}
	}
	
	public String execute() {
		List<MarketGroup> blueprintsMarketGroups = marketGroupUtils.getAllRelatedItems(new Integer(Constants.getInstance().getProperty(Constants.PropertyNames.BLUEPRINTS_MARKET_GROUP_ID)), marketGroupUtils.CHILDREN);
		
		ApiAuthorization auth = new ApiAuthorization(356078, (charID == null) ?  527926598 : charID, "EI4VWRRYN6mGGcTMIrfijnpTSbSmSulq5q5deAjUOq5Zm7DcjHWxEmR990BKWsZo");
		AssetListParser alp = AssetListParser.getInstance();
		try {
			AssetListResponse alr = alp.getResponse(auth);
			if(!alr.hasError()) {
				HibernateTemplate ht = new HibernateTemplate(sessionFactory);
				
				for(ApiAsset aa : alr.getAssets()) {
					out(aa, "", ht);
				}
			}
		} catch (ApiException e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

	public Account getA() {
		return a;
	}

	public CharacterSheetResponse getChrSheetResponse() {
		return chrSheetResponse;
	}

	public void setChrSheetResponse(CharacterSheetResponse chrSheetResponse) {
		this.chrSheetResponse = chrSheetResponse;
	}

	public Integer getCharID() {
		return charID;
	}

	public void setCharID(Integer charID) {
		this.charID = charID;
	}

	public Collection<ApiAsset<ApiAsset<?>>> getAssets() {
		return assets;
	}

	public void setAssets(Collection<ApiAsset<ApiAsset<?>>> assets) {
		this.assets = assets;
	}

	public KillLogResponse getKilResponse() {
		return kilResponse;
	}

	public void setKilResponse(KillLogResponse kilResponse) {
		this.kilResponse = kilResponse;
	}

	public CharactersResponse getChrResponse() {
		return chrResponse;
	}

	public void setChrResponse(CharactersResponse chrResponse) {
		this.chrResponse = chrResponse;
	}

	public AccountStatusResponse getAccResponse() {
		return accResponse;
	}

	public void setAccResponse(AccountStatusResponse accResponse) {
		this.accResponse = accResponse;
	}

	public User getUser() {
		return CurrentUser.getUser();
	}
}
